<?php
if(array_key_exists("HTTP_REFERER", $_SERVER)){
    $origin=substr($_SERVER['HTTP_REFERER'],0,strlen($_SERVER['HTTP_REFERER'])-1);
    header("Access-Control-Allow-Origin:".$origin);
    header("Access-Control-Allow-Credentials:true");
}
else
    header("Access-Control-Allow-Origin: *");
//header("Access-Control-Allow-Origin:http://localhost:8080");
header('Access-Control-Allow-Methods:POST,GET,PUT,DELETE'); // 允许请求的类型

include_once('DB.php');
include_once("ReturnHelper.php");
include_once("controller/BaseController.php");
include_once("controller/QuestionController.php");
include_once("controller/AnswerController.php");
include_once("controller/AnswerThumbUpController.php");
include_once("controller/CommentController.php");
include_once("controller/CommentReplyController.php");
include_once("controller/UserController.php");
include_once("controller/UserFollowController.php");
include_once("controller/UserCollectController.php");
include_once("controller/LoginController.php");
include_once("controller/RegisterController.php");
include_once("controller/VerificationController.php");
include_once("controller/ProvinceController.php");
include_once("controller/IndustryController.php");
include_once("controller/UploadController.php");

$method = $_SERVER['REQUEST_METHOD'];
session_start();
$lifeTime = 24 * 3600;
setcookie(session_name(), session_id(), time() + $lifeTime, "/");
if(!isset($_SESSION['isLogin'])){
    $_SESSION['isLogin']=false;
    $_SESSION['user_id']=null;
}

function getControllerName($controllerStr) {
    $parts = explode("_",$controllerStr);
    $res = "";
    for ($i = 0; $i < count($parts); $i++) {
        $res .= ucfirst($parts[$i]);
    }
    $res .= "Controller";
    return $res;
}


if (array_key_exists("s", $_GET)) {
    $path = $_GET["s"];
    $split = explode('/', $path);
    $isList = (count($split) === 2);
    $controllerStr = $split[0];
    $id = null;
    if ($isList === false) {
        $id = $split[1];
    }

    $controllerClassName = getControllerName($controllerStr);
    $controller = new $controllerClassName;

    $data = null;

    switch ($method) {
        case "GET": {
                if ($isList) {
                    $data = $controller->getList();
                } else {
                    $data = $controller->get($id);
                }
            }
            break;
        case "POST": {
                $raw = file_get_contents('php://input');
                $obj = json_decode($raw, true);
                $data = $controller->post($obj);
            }
            break;
        case "PUT": {
                $raw = file_get_contents('php://input');
                $obj = json_decode($raw, true);
                $data = $controller->update($id, $obj);
            }
            break;
        case "DELETE": {
                $data = $controller->delete($id);
            }
            break;
    }
    // 返回json
    ReturnHelper::response($data);
}
else if (array_key_exists("t", $_GET)) {
    $path = $_GET["t"];
    $split = explode('/', $path);
    $controllerStr = $split[0];

    $controllerClassName = getControllerName($controllerStr);
    $controller = new $controllerClassName;

    $functionName = $split[1];
    array_splice($split, 0, 2);
    array_splice($split, count($split) - 1, 1);

    $data = null;
    switch ($method) {
        case "GET":
        case "DELETE":
            // 调用相应的方法并传入参数
            $data = call_user_func_array(array($controller, $functionName), $split);
            break;
        case "POST":
        case "PUT":
            $raw = file_get_contents('php://input');
            $obj = json_decode($raw, true);
            // 将post或PUT的obj作为最后一个参数传入
            array_push($split, $obj);
            $data = call_user_func_array(array($controller, $functionName), $split);
            break;
    }
    // 返回json
    ReturnHelper::response($data);
}



